<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="danger" size='small' @click="clearAll" class="clearAll">{{$t('清除所有')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" style="margin-right:20px;" class="deleteData">{{$t('删除用户')}}</el-button>
      {{$t('刷新间隔：')}}
      <el-input v-emoji v-model="refreshPage" ref="refreshPage" size='small' style="margin-right:10px;" :style="lang === 'zh'?'width:120px;':'width:150px;'">
        <template slot="append">{{$t('秒')}}</template>
      </el-input>
      <el-button type="success" size='small' @click="startRefreshClick" v-if="!timer" class="Start">{{$t('开始刷新')}}</el-button>
      <el-button type="danger" size='small' @click="stopRefreshClick" v-else class="stop">{{$t('停止刷新')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="onlineList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="name" align='center' :label="$t('名称')" min-width="80px">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.name" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="server" align='center' :label="$t('认证服务器名称')" min-width="80px">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.server" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="map-info" align='center' :label="$t('地址')" min-width="100px">
        <template slot-scope="scope">
          <tooltip :copyText="mapInfoFormatter(scope.row)" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column align='center' width='100' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' + (scope.$index + 1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <online-dialog v-if="onlineDialogVisible" ref="refOnlineDialogVisible"></online-dialog>
  </div>
</template>
<script>

import { getUserArray, delUser, clearUser } from '@/api/user/online'
import OnlineDialog from './components/OnlineDialog.vue'
export default {
  name: 'Online',
  components: {
    OnlineDialog
  },
  data () {
    return {
      onlineDialogVisible: false,
      lang: this.$i18n.locale,
      onlineList: [],
      nameStr: [],
      refreshPage: 1,
      timer: null
    }
  },
  created () {
    this.listData()
  },
  mounted () {
    this.$refs.refreshPage.$refs.input.classList.value += ' refreshPage'
  },
  beforeDestroy () {
    if (this.timer) {
      clearInterval(this.timer) // 在Vue实例销毁前，清除我们的定时器
      this.timer = null
    }
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      this.nameStr = data
    },
    // 列表
    listData () {
      getUserArray().then(res => {
        if (res.status === 0) {
          this.onlineList = res.info
        }
      }).catch(e => {
        if (this.timer) {
          clearInterval(this.timer)
          this.timer = null
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.onlineDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refOnlineDialogVisible.init(data)
      })
    },
    clearAll () {
      this.$confirm(this.$t('您是否要清除所有用户？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        clearUser().then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('清除成功'),
              type: 'success'
            })
            this.nameStr = ''
            this.listData()
          }
        })
      })
    },
    deleteData () {
      if (this.nameStr && this.nameStr.length > 0) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delUser({ rule_map: JSON.stringify(this.nameStr) }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    startRefreshClick () {
      if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
      if (isNaN(Number(this.refreshPage))) {
        this.$messages.error(this.$t('只能输入数字'))
      } else if (Number(this.refreshPage) > 60) {
        this.$messages.error(this.$t('刷新频率不能超过60秒'))
      } else if (Number(this.refreshPage) < 1) {
        this.$messages.error(this.$t('刷新频率不能低于1秒'))
      } else {
        this.listData()
        this.timer = setInterval(() => {
          this.listData()
        }, this.refreshPage * 1000)
      }
    },
    stopRefreshClick () {
      if (this.timer) {
        clearInterval(this.timer) // 在Vue实例销毁前，清除我们的定时器
        this.timer = null
      }
    },
    mapInfoFormatter (data) {
      let str = ''
      for (let index in data['map-info']) {
        if (index === '0' || index === 0) {
          str += data['map-info'][index].ip
        } else {
          str += ',' + data['map-info'][index].ip
        }
      }
      return str
    }
  }
}
</script>
